package com.twu.biblioteca.menu;

import com.twu.biblioteca.Librarian;

public class ReturnInfoMenu implements Menu {
    public static final String THANK_YOU_FOR_RETURNING_THE_BOOK = "Thank you for returning the book.";
    public static final String SORRY_THAT_BOOK_IS_NOT_AVAILABLE = "Sorry! That book is not available.";
    public static final String THANK_YOU_FOR_RETURNING_THE_MOVIE = "Thank you for returning the movie.";
    public static final String SORRY_THAT_MOVIE_IS_NOT_AVAILABLE = "Sorry! That movie is not available.";
    private Librarian librarian;
    private String type;

    public ReturnInfoMenu(String type, Librarian librarian) {
        this.librarian = librarian;
        this.type = type;
        showMenu();
    }

    @Override
    public void showMenu() {
        if (type.equals("Book")) {
            System.out.println("Please enter the index of the book:");
            librarian.listBooks();
        } else if (type.equals("Movie")) {
            System.out.println("Please enter the index of the movie:");
            librarian.listMovies();
        }
    }

    @Override
    public Menu processInput(String input) {
        if (type.equals("Book") && (Integer.parseInt(input) <= librarian.getBookNumbers()) ) {
                return getMenu(
                        THANK_YOU_FOR_RETURNING_THE_BOOK,
                        SORRY_THAT_BOOK_IS_NOT_AVAILABLE,
                        librarian.returnBook(input)
                );

        } else if (type.equals("Movie") && (Integer.parseInt(input) <= librarian.getMoviesNumbers())) {
                return getMenu(
                        THANK_YOU_FOR_RETURNING_THE_MOVIE,
                        SORRY_THAT_MOVIE_IS_NOT_AVAILABLE,
                        librarian.returnMovie(input)
                );
        } else {
            System.out.println("Sorry, please re-enter.");
            return this;
        }
    }

    private Menu getMenu(String successWords, String failedWords, boolean success) {
        if (success) {
            System.out.println(successWords);
            return new LibraryMenu(librarian);
        } else {
            System.out.println(failedWords);
            return new ReturnMenu(librarian);
        }
    }
}
